home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / log / undoTransaction.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  9.4 KB  |  362 lines

  1. /*
  2.  *   $RCSfile: undoTransaction.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:50 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "pool.h"
  45. #include "tid.h"
  46. #include "io.h"
  47. #include "lock.h"
  48. #include "object.h"
  49. #include "msgdefs.h"
  50. #include "thread.h"
  51. #include "semaphore.h"
  52. #include "latch.h"
  53. #include "link.h"
  54. #include "lsn.h"
  55. #include "bf.h"
  56. #include "log.h"
  57. #include "volume.h"
  58. #include "openlog.h"
  59. #include "trans.h"
  60. #include "logrecs.h"
  61. #include "undo.h"
  62. #include "threadstate.h"
  63. #include "bf_extfuncs.h"
  64. #include "util_funcs.h"
  65. #include "log_intfuncs.h"
  66. #include "log_extfuncs.h"
  67. #include "undo_extfuncs.h"
  68. #include "thread_globals.h"
  69. #include "io_globals.h"
  70. #include "log_globals.h"
  71. #include "trans_globals.h"
  72. #include "undo_globals.h"
  73.  
  74.  
  75.  void
  76. undoTransaction (
  77.  
  78.     LSNOFFSET        limitLSN,
  79.     LSNOFFSET        startLSN, /* NULL_LSN => use last transaction LSN */
  80.     FLAGS            flags 
  81. )
  82. {
  83.  
  84.     register LOGRECORDHDR    *recordHeader;
  85.     register GROUPLINK        *groupLink;
  86.     register PAGE2SIZE        page2size;
  87.     register LSNOFFSET        nextLSN;
  88.     register LSNOFFSET        offset;
  89.     LSNOFFSET                lastLSN;
  90.     OPENLOG                    *openLog;
  91.     PID                        pid;
  92.     TRANSREC                *transRec;
  93.     LOGPAGEHDR                *pageHeader;
  94.     LOGPAGEHDR                *endHeader;
  95.     GROUPLINK                *logRecordBuffer = NULL;
  96. #ifdef DEBUG
  97.     LOGRECORDHDR            *previousRecord;
  98.     LOGPAGEHDR                tempHeader;
  99. #endif
  100.  
  101.  
  102.     TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_1, ("limitLSN:%d", limitLSN));
  103.  
  104.     PREVENT_FAST_IO();
  105.  
  106.     /*
  107.      *    put values in registers
  108.      */
  109.     openLog      = &OpenLog;
  110.     pid.volid = openLog->volid;
  111.     page2size = openLog->page2size;
  112.     transRec  = (TRANSREC*) Active->transRec;
  113.     PRINT_PROGRESS(("Undoing transaction:%d\n", transRec->tid));
  114.  
  115.     /*
  116.      *    If the requested starting log record is NULL and the 
  117.      *    transaction is being undone in normal operations (ie. not
  118.      *    during recovery) then use the last log record generated by
  119.      *    the transaction as the starting point.
  120.      */
  121.     SM_ASSERT(LEVEL_3, ((flags & LOG_RECOVERY) == 1) == (TransInfo.logState == L_RECOVER));
  122.     if ((startLSN == NULL_LSN) && !(flags & LOG_RECOVERY) ) {
  123.         lastLSN      = transRec->lastLSN;
  124.     } else {
  125.         lastLSN      = startLSN;
  126.     }
  127.  
  128.  
  129. #ifndef INIT_LRC_IS_LSN
  130.     /*
  131.      *    set up structures for recording pages whose updates are missing
  132.      */
  133.     transRec->missingUpdateInfo = setupMissingUpdateInfo();
  134.     if (transRec->missingUpdateInfo == NULL) {
  135.         SM_ERROR(TYPE_FATAL, esmINTERNAL);
  136.     }
  137. #endif INIT_LRC_IS_LSN
  138.  
  139.     /*
  140.      *    while there are records process the undo routines
  141.      */
  142.     recordHeader = NULL;
  143.     while (lastLSN != limitLSN)    {
  144.  
  145.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_2, ("lastLSN:%d", lastLSN));
  146.  
  147.         /*
  148.          *    get the latch on the log
  149.          */
  150.         if (waitLatch( &(openLog->logLatch), SHARE_LATCH ))    {
  151.  
  152.             SM_ERROR(TYPE_FATAL, Active->errno);
  153.         }
  154.  
  155.         /*
  156.          *    check to see if the log is in use
  157.          *
  158.          *    Note that this logic should be changed to let more than
  159.          *    a single aborter be active.  This would involve handling
  160.          *    buffer group overflow and coordinating undo buffer access.
  161.          */
  162.         if (waitSemaphore( &(openLog->readSemaphore) ))    {
  163.  
  164.             SM_ERROR(TYPE_FATAL, Active->errno);
  165.         }
  166.  
  167.         /*
  168.          *    calculate the page that will be read in disk blocks
  169.          */
  170.         pid.page = LSN_TO_BLOCK(lastLSN, openLog);
  171.  
  172.         /*
  173.          *    attempt to read the page
  174.          */
  175.         if ((groupLink = bf_ReadPage(openLog->readGroup, &pid,
  176.                                      page2size, NOFLAGS)) == NULL)    {
  177.  
  178.             SM_ERROR(TYPE_FATAL, Active->errno);
  179.         }
  180.  
  181.         /*
  182.          *    setup pointers to the page head and tail
  183.          */
  184.         pageHeader = (LOGPAGEHDR *) groupLink->bufFrame;
  185.         endHeader  = (LOGPAGEHDR *) (((char *) pageHeader) + openLog->lastUsableByte);
  186.  
  187. #ifdef DEBUG
  188.         tempHeader = *pageHeader;
  189. #endif
  190.         /*
  191.          *    check that the page numbers are correct
  192.          */
  193.         SM_ASSERT(LEVEL_1, pageHeader->pageNumber ==  LSN_TO_LOG_PAGE(lastLSN, openLog));
  194.         SM_ASSERT(LEVEL_1, endHeader->pageNumber ==  LSN_TO_LOG_PAGE(lastLSN, openLog));
  195.  
  196.         /*
  197.          *    check the magic numbers
  198.          */
  199.         if (CHECK_LOGPAGE_MAGIC(pageHeader) || CHECK_LOGPAGE_MAGIC(endHeader))    {
  200.  
  201.             SM_ERROR(TYPE_FATAL, esmBADLOGPAGEHEADER);
  202.         }
  203.  
  204.  
  205.         /*
  206.          *    get the offset of the record
  207.          */
  208.         offset = LOG_PAGE_OFFSET(lastLSN, openLog);
  209.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_2, ("offset:%d", offset));
  210.         SM_ASSERT(LEVEL_3, offset <= pageHeader->lastRecord);
  211.  
  212.         /*
  213.          *    get a pointer to the record header
  214.          */
  215. #ifdef DEBUG
  216.         previousRecord = recordHeader;
  217. #endif DEBUG
  218.         recordHeader = (LOGRECORDHDR *) (groupLink->bufFrame + offset);    
  219.  
  220.         /*
  221.          *    check the record header magic, and make sure it belongs
  222.          *    to the transaction being undone
  223.          */
  224.         SM_ASSERT(LEVEL_1, !(CHECK_LOGRECORD_MAGIC(recordHeader)));
  225.         SM_ASSERT(LEVEL_1, transRec->tid == recordHeader->tid);
  226.  
  227.         /*
  228.          *    print out the record information
  229.          */
  230.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("tid:%x", recordHeader->tid));
  231.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("LSN:%d, LRC:%d", 
  232.                 recordHeader->recordLSN.offset, recordHeader->actionLRC.count));
  233.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("length:%d", recordHeader->length));
  234.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("type:%d", recordHeader->type));
  235.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("action:%d, actionPid:%d", 
  236.                 recordHeader->action, recordHeader->actionPid.page));
  237.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("imageCount:%d", recordHeader->imageCount));
  238.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("prevLSN:%d", recordHeader->previousLSN));
  239.         TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("undoLSN:%d", recordHeader->nextUndoLSN));
  240.  
  241. #if TRACING IS_ENABLED
  242.  
  243.         {
  244.             register int    i;
  245.  
  246.             for (i = 0; i < recordHeader->imageCount; i++)    {
  247.  
  248.                 TRPRINT(TR_LOG|TR_RECOVER, TR_LEVEL_3, ("offset:%d size:%d",
  249.                         recordHeader->imageOffset[i], recordHeader->imageSize[i]));
  250.             }
  251.         }
  252.  
  253. #endif
  254.     
  255.         /*
  256.          *    get a pointer to the next log record to undo
  257.          */
  258.         if (recordHeader->type == LOG_REC_TYPE_COMPENSATION ||
  259.             recordHeader->type == LOG_REC_TYPE_USER_COMPENSATION)    {
  260.  
  261.             nextLSN = recordHeader->nextUndoLSN;
  262.             TRPRINT(TR_LOG, TR_LEVEL_3, ("compensation undoNextLSN:%d", nextLSN));
  263.  
  264.         } else {
  265.  
  266.             nextLSN = recordHeader->previousLSN;
  267.             TRPRINT(TR_LOG, TR_LEVEL_3, ("nextLSN:%d", nextLSN));
  268.         }
  269.  
  270.         if ((recordHeader->type == LOG_REC_TYPE_COMMIT) ||
  271.             (recordHeader->type == LOG_REC_TYPE_END)) {
  272.             /*
  273.              *  We do not need to undo the the transaction since
  274.              *  it committed.  Set things up so that we exit the
  275.              *  loop.
  276.              */
  277.             nextLSN = limitLSN;
  278.  
  279.         } 
  280.         else if ((recordHeader->type == LOG_REC_TYPE_SERVER_PREPARE) ||
  281.                  (recordHeader->type == LOG_REC_TYPE_COORD_PREPARE)) {
  282.             /*
  283.              *    The fact that we are here means that we are definitely
  284.              *    undoing this transaction - so skip these reccords 
  285.              *    and proceed to undo the rest
  286.              */
  287.         } else if ((offset + recordHeader->length) <= openLog->lastUsableByte)  {
  288.  
  289.             /*
  290.              *  the record resides entirely on this page so
  291.              *  we can directly undo the action
  292.              */
  293.             undoAction(recordHeader);
  294.  
  295.         } else {
  296.             
  297.             SM_ASSERT(LEVEL_3, logRecordBuffer == NULL);    
  298.             logRecordBuffer = getLogRecordBuffer(recordHeader->length);
  299.  
  300.             /*
  301.              *    the log record must be copied into contiguous space in the
  302.              *    undo buffer provided for the log
  303.              */
  304.             if (combineRecord(openLog, recordHeader, (openLog->lastUsableByte - offset), TRUE, logRecordBuffer) != esmNOERROR) {
  305.  
  306.                 /*
  307.                  *    Check to make sure that this is recovery.  In this case we
  308.                  *    just ignore this record and proceed to the next
  309.                  */
  310.                 if (flags & LOG_RECOVERY)    {
  311.  
  312.                     SM_ERROR(TYPE_LOG, esmINTERNAL);
  313.  
  314.                 } else {
  315.  
  316.                     SM_ERROR(TYPE_FATAL, esmINTERNAL);
  317.                 }
  318.  
  319.             } else {
  320.  
  321.                 /*
  322.                  *    undo the action
  323.                  */
  324.                 undoAction((LOGRECORDHDR *) logRecordBuffer->bufFrame);
  325.             }
  326.  
  327.             freeLogRecordBuffer(logRecordBuffer);    
  328.             logRecordBuffer = NULL;
  329.         }
  330.  
  331.         /*
  332.          *    get a new last lsn
  333.          */
  334.         lastLSN = nextLSN;
  335.  
  336.         /*
  337.          *    release the page
  338.          */
  339.         bf_UnfixPage(groupLink, BF_DEFAULT, FALSE);        
  340.  
  341.         /*
  342.          *    release semaphore and latch
  343.          */
  344.         signalSemaphore( &(openLog->readSemaphore) );
  345.         signalLatch( &(openLog->logLatch) );
  346.     }
  347.  
  348. #ifndef INIT_LRC_IS_LSN
  349.     /*
  350.      *    If information was maintained on missing updates,
  351.      *    free up the information
  352.      */
  353.     if (transRec->missingUpdateInfo) {
  354.         freeMissingUpdateInfo();
  355.     }
  356. #endif INIT_LRC_IS_LSN
  357.  
  358.     ALLOW_FAST_IO();
  359.  
  360.     SM_ASSERT(LEVEL_3, logRecordBuffer == NULL);
  361. }
  362.